package com.thinksns.sociax.t4.android.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.adapter.AdapterFindPeopleByTag;
import com.thinksns.sociax.t4.adapter.AdapterSociaxList;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.user.ActivityUserInfo_2;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.model.ModelSearchUser;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.thinksnsbase.bean.ListData;
import com.thinksns.sociax.thinksnsbase.bean.SociaxItem;

/**
 * 类说明： 根据标签找人
 *
 * 需要传入intent int tag_id
 *
 * @author wz
 * @version 1.0
 * @date 2014-11-4
 */
public class FragmentFindPeopleByTag extends FragmentSociax implements OnRefreshListener,OnLoadmoreListener {
    protected int selectpostion;

    private int tag_id;
    private SmartRefreshLayout smartRefreshLayout;

    @Override
    public void initView() {
        smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smart_refresh_layout);
        listView = (ListView) findViewById(R.id.pull_refresh_list);
        listView.setDivider(new ColorDrawable(getResources().getColor(R.color.bg_ios)));
        listView.setDividerHeight(1);
        listView.setSelector(getResources().getDrawable(R.drawable.listitem_selector));

        adapter = createAdapter();
        listView.setAdapter(adapter);
        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableLoadmore(true);
    }

    @Override
    public AdapterSociaxList createAdapter() {
        list = new ListData<SociaxItem>();
        return new AdapterFindPeopleByTag(this, list, getActivity()
                .getIntent().getIntExtra("uid", Thinksns.getMy().getUid()),
                tag_id);
    }

    @Override
    public void initIntentData() {
        if (getActivity().getIntent().hasExtra("tag_id")) {
            tag_id = getActivity().getIntent().getIntExtra("tag_id", 0);
        } else {
            Toast.makeText(getActivity(), "操作失败", Toast.LENGTH_SHORT).show();
            getActivity().finish();
        }
    }

    @Override
    public void initListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ModelSearchUser user = (ModelSearchUser) adapter.getItem((int) id);
                if (Thinksns.getMy().getUid() != user.getUid() && ModelUser.CAN_NOT_TO_HOME == user.getSpace_privacy()){
                    ToastUtils.showToast(R.string.tip_no_oauth_to_other_home);
                }else {
                    Intent intent = new Intent(getActivity(), ActivityUserInfo_2.class);
                    intent.putExtra("uid", user.getUid());
                    startActivity(intent);
                }
            }
        });
        smartRefreshLayout.setOnLoadmoreListener(this);
    }

    @Override
    public View getDefaultView() {
        return findViewById(R.id.default_people_bg);
    }

    @Override
    public void initData() {
        adapter.loadInitData();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_chat_userlist;
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {

        if (adapter != null){
            adapter.doRefreshFooter();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {

    }

    @Override
    public void executeDataSuccess(ListData<SociaxItem> list) {
        super.executeDataSuccess(list);
        smartRefreshLayout.finishLoadmore();
    }
}
